<template>
	<div class="collect_main">
		<div class="list" v-if="listData.length!=0">
			<div class="item" v-for="(item,index) in listData" :key="index">
	        	<img :src="item.list_pic_url"  @click="goodsDetail(item.id)">
	        	<div  >
	        		<p class="name" @click="goodsDetail(item.id)">{{item.name}}</p>
	        		<p class="price" @click="goodsDetail(item.id)">¥ {{item.retail_price}}</p>
	        		<p class="cancel" @click="delCollect(item.id)">取消收藏</p>
	        	</div>	        		      	
    		</div>	 
		</div>  	
		<div class="no" v-else>
			<div class="img">
				<img src="/static/images/no.png">
			</div>
			<div class="text">暂无收藏商品，快去逛逛吧~</div>
			<div class="go" @click="goIndex">去首页看好物</div>
		</div>	   		    
	</div>
</template>
<script type="text/javascript">
import {get,post} from '@/util'
export default{
	data() {
		return {
			listData:[],
			openId:'o_MyB4jDjNC9HMf-CDmLApTovzro'
		}
	},
	// mounted
	mounted(){
		const userinfo = wx.getStorageSync('userinfo')
	    // this.openId = userinfo.openId
	    this.getData()
	},
	methods:{
		async getData(){
			const data = await get('/app/collect/collectList',{
				openId:this.openId
			})
			this.listData = data.listData
		},
		//点击某商品跳转到详情页
	    goodsDetail(id){
	    	console.log("传去的id：",id)
	    	wx.navigateTo({
		        url: '/pages/goods/main?id=' + id
		    })
	    },
	    async delCollect(id){
	    	const data = await post('/app/collect/delCollect',{
	    		openId:this.openId,
	    		goodsId:id
	    	})
	    	if(data){    		
	    		wx.showToast({
				  	title: '取消成功',
				  	icon: 'none',
				  	duration: 2000,
				  	success: res => {
				  		this.getData()  
				  	}
				})	  					
	    	}else{
	    		console.log("收藏取消失败")
	    	}
	    	
	    },
    	goIndex(){
			wx.switchTab({
        		url: '/pages/index/main'
	     	})
		},
	}
}
</script>
<style lang="scss">
@import "./style.scss";
</style>